{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_youtube('_6JOA-iiEGU')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Download an ee.Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = ee.Image('LE7_TOA_5YEAR/1999_2003')\n",
    "\n",
    "landsat_vis = {\n",
    "    'bands': ['B4', 'B3', 'B2'], \n",
    "    'gamma': 1.4\n",
    "}\n",
    "Map.addLayer(image, landsat_vis, \"LE7_TOA_5YEAR/1999_2003\", True, 0.7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Draw any shapes on the map using the Drawing tools before executing this code block\n",
    "feature = Map.draw_last_feature\n",
    "roi = feature.geometry()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')\n",
    "filename = os.path.join(out_dir, 'landsat.tif')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Exporting all bands as one single image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_image(image, filename=filename, scale=90, region=roi, file_per_band=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Exporting each band as one image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_image(image, filename=filename, scale=90, region=roi, file_per_band=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Download an ee.ImageCollection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "loc = ee.Geometry.Point(-99.2222, 46.7816)\n",
    "collection = ee.ImageCollection('USDA/NAIP/DOQQ') \\\n",
    "    .filterBounds(loc) \\\n",
    "    .filterDate('2008-01-01', '2020-01-01') \\\n",
    "    .filter(ee.Filter.listContains(\"system:band_names\", \"N\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_export_image_collection(collection, out_dir=out_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Extract pixels as a Numpy array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "img = ee.Image('LANDSAT/LC08/C01/T1_SR/LC08_038029_20180810') \\\n",
    "  .select(['B4', 'B5', 'B6'])\n",
    "\n",
    "aoi = ee.Geometry.Polygon(\n",
    "  [[[-110.8, 44.7],\n",
    "    [-110.8, 44.6],\n",
    "    [-110.6, 44.6],\n",
    "    [-110.6, 44.7]]], None, False)\n",
    "\n",
    "rgb_img = geemap.ee_to_numpy(img, region=aoi)\n",
    "print(rgb_img.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Scale the data to [0, 255] to show as an RGB image. \n",
    "# Adapted from https://bit.ly/2XlmQY8. Credits to Justin Braaten\n",
    "rgb_img_test = (255*((rgb_img[:, :, 0:3] - 100)/3500)).astype('uint8')\n",
    "plt.imshow(rgb_img_test)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
